using System;
using System.Collections.Generic;
using System.Globalization;
using RaisingStudio.SmallBasicCompiler;

namespace RaisingStudio.SmallBasicCompiler
{
	public class SymbolTable
	{
		private List<Error> _errors;
		public List<Error> Errors
		{
			get
			{
				return this._errors;
			}
		}

		private Dictionary<string, TokenInfo> _initializedVariables = new Dictionary<string, TokenInfo>();
		public Dictionary<string, TokenInfo> InitializedVariables
		{
			get
			{
				return this._initializedVariables;
			}
		}

		private Dictionary<string, TokenInfo> _variables = new Dictionary<string, TokenInfo>();
		public Dictionary<string, TokenInfo> Variables
		{
			get
			{
				return this._variables;
			}
		}

		private Dictionary<string, TokenInfo> _subroutines = new Dictionary<string, TokenInfo>();
		public Dictionary<string, TokenInfo> Subroutines
		{
			get
			{
				return this._subroutines;
			}
		}

		private Dictionary<string, TokenInfo> _labels = new Dictionary<string, TokenInfo>();
		public Dictionary<string, TokenInfo> Labels
		{
			get
			{
				return this._labels;
			}
		}

		public SymbolTable(List<Error> errors)
		{
			this._errors = errors;
			if (this._errors == null)
			{
				this._errors = new List<Error>();
			}
		}

		public void Reset()
		{
			this._errors.Clear();
			this._labels.Clear();
			this._subroutines.Clear();
			this._variables.Clear();
		}

		public void AddVariable(TokenInfo variable)
		{
			if (!this.Variables.ContainsKey(variable.NormalizedText))
			{
				this.Variables.Add(variable.NormalizedText, variable);
			}
		}
		public void AddVariableInitialization(TokenInfo variable)
		{
			if (!this.InitializedVariables.ContainsKey(variable.NormalizedText))
			{
				this.InitializedVariables.Add(variable.NormalizedText, variable);
			}
		}
		public void AddSubroutine(TokenInfo subroutineName)
		{
			string normalizedText = subroutineName.NormalizedText;
			if (this.Variables.ContainsKey(normalizedText))
			{
				this.Variables.Remove(normalizedText);
			}
			if (!this.Subroutines.ContainsKey(normalizedText))
			{
				this.Subroutines.Add(normalizedText, subroutineName);
				return;
			}
			this.Errors.Add(new Error(subroutineName, string.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("AnotherSubroutineExists"), new object[]
			{
				subroutineName.Text
			})));
		}
		public void AddLabelDefinition(TokenInfo label)
		{
			string normalizedText = label.NormalizedText;
			if (!this.Labels.ContainsKey(normalizedText))
			{
				this.Labels.Add(normalizedText, label);
				return;
			}
			this.Errors.Add(new Error(label, string.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("AnotherLabelExists"), new object[]
			{
				label.Text
			})));
		}
	}
}
